using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace BooStudio
{
	public class RewardUIController : MonoBehaviour
	{
		public Transform animatedGiftBox;

		public GameObject congratsText;

		public GameObject sunburst;

		public GameObject reward;

		public GameObject coinIcon;

		public Text rewardText;

		public bool animateReward = true;

		public GameObject rewardUIBG;

		private bool isRewarding;

		public static event Action RewardUIOpened;

		public static event Action RewardUIClosed;

		public void Reward(int rewardValue)
		{
			if (!isRewarding)
			{
				StartCoroutine(CRPlayRewardAnim(rewardValue));
			}
		}

		public void Close()
		{
			if (!isRewarding)
			{
				base.gameObject.SetActive(value: false);
				if (RewardUIController.RewardUIClosed != null)
				{
					RewardUIController.RewardUIClosed();
				}
				rewardUIBG.SetActive(value: false);
			}
		}

		private IEnumerator CRPlayRewardAnim(int rewardValue)
		{
			if (RewardUIController.RewardUIOpened != null)
			{
				RewardUIController.RewardUIOpened();
			}
			isRewarding = true;
			congratsText.SetActive(value: false);
			reward.SetActive(value: false);
			sunburst.SetActive(value: false);
			animatedGiftBox.gameObject.SetActive(value: true);
			float start2 = Time.time;
			while (Time.time - start2 < 2f)
			{
				animatedGiftBox.localEulerAngles = new Vector3(0f, 0f, UnityEngine.Random.Range(-10f, 10f));
				animatedGiftBox.localScale = new Vector3(UnityEngine.Random.Range(0.9f, 1.3f), UnityEngine.Random.Range(0.9f, 1.3f), UnityEngine.Random.Range(0.9f, 1.3f));
				yield return new WaitForSeconds(0.07f);
			}
			start2 = Time.time;
			Vector3 startScale = animatedGiftBox.localScale;
			while (Time.time - start2 < 0.15f)
			{
				animatedGiftBox.localScale = Vector3.Lerp(startScale, Vector3.one * 20f, (Time.time - start2) / 0.2f);
				yield return null;
			}
			animatedGiftBox.gameObject.SetActive(value: false);
			reward.SetActive(value: true);
			coinIcon.SetActive(value: true);
			for (int i = 1; i <= rewardValue; i++)
			{
				rewardText.text = "+" + i.ToString();
				yield return null;
			}
			CoinManager.coin += rewardValue;
			CoinManager.UpCoin();
			yield return new WaitForSeconds(0.2f);
			congratsText.SetActive(value: true);
			sunburst.SetActive(value: true);
			UnityEngine.Object.FindObjectOfType<RewardBurstAnimation>().ShowStartAnimation();
			if (animateReward)
			{
				reward.GetComponent<Animator>().SetTrigger("Reward");
			}
			yield return new WaitForSeconds(1f);
			isRewarding = false;
			yield return new WaitForSeconds(1f);
			if (base.gameObject.activeSelf)
			{
				Close();
			}
		}
	}
}
